
package com.productmodule.servlet;
import com.productmodule.bean.TbProduct;
import com.productmodule.dao.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbProductServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        if("add".equals(method)){
        int productTypeId=Integer.parseInt(request.getParameter("productTypeId"));
        String productName=request.getParameter("productName");
        String interfaceId=request.getParameter("interfaceId");
        String areaId=request.getParameter("areaId");
        int userId=Integer.parseInt(request.getParameter("userId"));
        String productMemo=request.getParameter("productMemo");
        String createTime=request.getParameter("createTime");
            if(new TbProductDAO().add(new TbProduct(productTypeId,productName,interfaceId,areaId,userId,productMemo,createTime))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
             request.getSession(true).setAttribute("tbProductList",new TbProductDAO().getTbProductAll());
            response.sendRedirect("ProductModule/TbProductList.jsp");
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_Product", "ProductId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
             request.getSession(true).setAttribute("tbProductList",new TbProductDAO().getTbProductAll());
            response.sendRedirect("ProductModule/TbProductList.jsp");
        }
        if("edit".equals(method)){
        int productId=Integer.parseInt(request.getParameter("productId"));
        int productTypeId=Integer.parseInt(request.getParameter("productTypeId"));
        String productName=request.getParameter("productName");
        String interfaceId=request.getParameter("interfaceId");
        String areaId=request.getParameter("areaId");
        int userId=Integer.parseInt(request.getParameter("userId"));
        String productMemo=request.getParameter("productMemo");
        String createTime=request.getParameter("createTime");
            if(new TbProductDAO().edit(new TbProduct(productId,productTypeId,productName,interfaceId,areaId,userId,productMemo,createTime))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
             request.getSession(true).setAttribute("tbProductList",new TbProductDAO().getTbProductAll());
            response.sendRedirect("ProductModule/TbProductList.jsp");
        }
        if ("list".equals(method)) {
             request.getSession(true).setAttribute("tbProductList",new TbProductDAO().getTbProductAll());
            response.sendRedirect("ProductModule/TbProductList.jsp");
        	}
        if ("listById".equals(method)) {
            request.getSession(true).setAttribute("tbProduct",new TbProductDAO().getTbProductByProductId(Integer.parseInt(request.getParameter("productId"))));
            response.sendRedirect("ProductModule/TbProductOperate.jsp");
    	}
        if("showadd".equals(method)){
        	//查询所有供应商信息
        	request.getSession(true).removeAttribute("tbProduct");
            response.sendRedirect("ProductModule/TbProductOperate.jsp");
        }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
